//
//  AskForHelpViewController.m
//  HHBroker
//
//  Created by He Wei on 9/29/15.
//  Copyright © 2015 harry. All rights reserved.
//

#import "AskForHelpViewController.h"
#import "AskForHelpInfoCell.h"
#import "AskForHelpInfoModel.h"
#import "UIView+callingAlertview.h"


@interface AskForHelpViewController ()

@property (nonatomic, strong) UIView *footerView;
@property (nonatomic, strong) UIButton *returnButton;
@property (nonatomic, copy) NSString *phone;



@end

@implementation AskForHelpViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self prepareData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Delegate
#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuseCell = @"reuseCell";
    AskForHelpInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseCell];
    if (cell == nil) {
        cell = [[AskForHelpInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseCell];
    }
    AskForHelpInfoModel *model = self.dataArray[indexPath.row];
    cell.model = model;
    return cell;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return self.footerView.height;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AskForHelpInfoModel *model = self.dataArray[indexPath.row];
    return [AskForHelpInfoCell heightWithModel:model];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor whiteColor];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 15, 200, 14)];
    NSString *string = @"常见问题";
    label.text = string;
    label.textColor = UIColorFromRGB(0x5d5d5d);
    [view addSubview:label];
    return view;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return self.footerView;

}

#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
    [self callANumber:self.phone];
    }
}

#pragma mark - Event

- (void)returnButtonClicked:(UIButton *)button
{
    self.phone = @"400-8217-950";
    UIView *view = [UIView callAphoneNumber:self.phone withTitle:nil];
    if ([view isKindOfClass:[UIAlertView class]]) {
        UIAlertView *alertView = (UIAlertView *)view;
        alertView.delegate = self;
        [alertView show];
    } else {
        [self.view addSubview:view];
    }
}


#pragma mark - Setter && Getter
- (void)prepareData
{
    MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSString *urlString = KUrlHelpCenter;
    [HttpRequestManager getWithUrlString:urlString success:^(id response) {
        NSInteger code = [[response objectForKey:@"code"] integerValue];
        if (code == 1) {
            [hub hide:YES];
            NSArray *array = [response objectForKey:@"data"];
            for (NSDictionary *dic in array) {
                AskForHelpInfoModel *model = [[AskForHelpInfoModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.dataArray addObject:model];
            }
            [self.tableView reloadData];
        } else {
            hub.mode = MBProgressHUDModeText;
            hub.labelText = [response objectForKey:@"msg"];
            [hub hide:YES afterDelay:1];
        
        }
    
    
    }failure:^(NSError *error) {
        hub.mode = MBProgressHUDModeText;
        hub.labelText = @"The Network has a problem";
        [hub hide:YES afterDelay:2];
    
    } cache:NO];
}

- (UIView *)footerView
{
    if (_footerView == nil) {
        _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.height-64, self.view.width, 140)];
        _footerView.backgroundColor  = [UIColor clearColor];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, self.view.width-40, 36)];
        label.font = [UIFont systemFontOfSize:15];
        label.textColor = UIColorFromRGB(0x999999);
        label.text = @"如果以上信息没有解决您的问题，欢迎拨打我们的咨询电话";
        label.textAlignment = NSTextAlignmentCenter;
        label.numberOfLines = 0;
        [_footerView addSubview:label];
        [_footerView addSubview:self.returnButton];
    }
    return _footerView;
}

- (UIButton *)returnButton
{
    if (_returnButton == nil) {
        _returnButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _returnButton.frame = CGRectMake(self.view.width-178/2.0, 50+15, 178, 44);
        [_returnButton setTitleColor:UIColorFromRGB(0x5d5d5d) forState:UIControlStateNormal];
        _returnButton.titleLabel.font = [UIFont systemFontOfSize:17];
        _returnButton.layer.cornerRadius = 5;
        _returnButton.layer.borderColor = UIColorFromRGB(0xc7c7c7).CGColor;
        _returnButton.layer.borderWidth = 0.5;
        [_returnButton setTitle:@"400-8217-950" forState:UIControlStateNormal];
        [_returnButton addTarget:self action:@selector(returnButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _returnButton;
}





@end
